﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using OConf.Entity;
using OConf.Model.Common;
using System;
using System.Linq;
using System.Linq.Expressions;

namespace OConf.Service
{
    public class ProjectInfoService
    {
        private IConfiguration _configuration { get; }
        public ProjectInfoService(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public bool IsExists(string key)
        {
            bool isExists = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                isExists = db.ProjectInfo.Any(w => w.ID == key);
            }
            return isExists;
        }

        public bool IsExists(string key, bool isDelete)
        {
            bool isExists = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                isExists = db.ProjectInfo.Any(w => w.ID == key && w.IsDelete == isDelete);
            }
            return isExists;
        }

        public bool IsExistsByProjectName(string projectName)
        {
            bool isExists = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                isExists = db.ProjectInfo.Any(w => w.ProjectName == projectName);
            }
            return isExists;
        }

        /// <summary>
        /// 新增对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool AddEntity(ProjectInfoEntity entity)
        {
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                db.ProjectInfo.Add(entity);
                isSuccess = db.SaveChanges() > 0;
            }
            return isSuccess;
        }

        /// <summary>
        /// 硬删除项目信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public void Delete(string key)
        {
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {

                        int rows = db.ProjectInfo.Where(w => w.ID == key && w.IsDelete == true).ExecuteDelete();

                        if (rows <= 0)
                        {
                            throw new Exception("项目信息删除失败");
                        }

                        db.CNameSpace.Where(w => w.ProjectID == key)
                            .ExecuteDelete();

                        db.ConfigInfo.Where(w => w.ProjectID == key)
                            .ExecuteDelete();

                        tran.Commit();
                    }
                    catch
                    {
                        tran.Rollback();
                        throw;
                    }
                }
            }
        }

        /// <summary>
        /// 软删除项目信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteSoft(string id)
        {
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                ProjectInfoEntity entity = new ProjectInfoEntity() 
                {
                    ID = id,
                    IsDelete = true
                };
                db.ProjectInfo.Attach(entity);
                db.Entry(entity).Property(p => p.IsDelete).IsModified = true;
                isSuccess = db.SaveChanges() > 0;
            }
            return isSuccess;
        }


        /// <summary>
        /// 修改基础信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateEntity(ProjectInfoEntity entity)
        {
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                db.ProjectInfo.Attach(entity);
                db.Entry(entity).Property(p => p.ProjectDescription).IsModified = true;
                db.Entry(entity).Property(p => p.IsProtect).IsModified = true;
                isSuccess = db.SaveChanges() > 0;
            }
            return isSuccess;
        }

        /// <summary>
        /// 修改秘钥
        /// </summary>
        /// <param name="id"></param>
        /// <param name="secretKey"></param>
        /// <returns></returns>
        public bool ModifySecretKey(string id, string secretKey)
        {
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                ProjectInfoEntity entity = new ProjectInfoEntity()
                {
                    ID = id,
                    IsProtect = true,
                    SecretKey = secretKey
                };

                db.ProjectInfo.Attach(entity);
                db.Entry(entity).Property(p => p.IsProtect).IsModified = true;
                db.Entry(entity).Property(p => p.SecretKey).IsModified = true;
                isSuccess = db.SaveChanges() > 0;
            }
            return isSuccess;
        }

        /// <summary>
        /// 还原项目信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Reduction(string id)
        {
            bool isSuccess = false;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                ProjectInfoEntity entity = new ProjectInfoEntity()
                {
                    ID = id,
                    IsDelete = false
                };
                db.ProjectInfo.Attach(entity);
                db.Entry(entity).Property(p => p.IsDelete).IsModified = true;
                isSuccess = db.SaveChanges() > 0;
            }
            return isSuccess;
        }

        public ProjectInfoEntity FindEnity(string key)
        {
            ProjectInfoEntity entity = null;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                entity = db.ProjectInfo.Where(w => w.ID == key).FirstOrDefault();
            }
            return entity;
        }

        /// <summary>
        /// 根据项目名称获取项目信息
        /// </summary>
        /// <param name="programName"></param>
        /// <returns></returns>
        public ProjectInfoEntity FindEnityByProgramName(string programName)
        {
            ProjectInfoEntity entity = null;
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                entity = db.ProjectInfo.Where(w => w.ProjectName == programName).FirstOrDefault();
            }
            return entity;
        }

        /// <summary>
        /// /分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public ReturnTableData<ProjectInfoEntity> QueryPage(int pageIndex, int pageSize, Expression<Func<ProjectInfoEntity, bool>> whereLambda)
        {
            ReturnTableData<ProjectInfoEntity> result = new ReturnTableData<ProjectInfoEntity>();
            using (MyDBContext db = new MyDBContext(_configuration))
            {
                var query = db.ProjectInfo.Where(whereLambda);

                result.Total = query.Count();

                if (result.Total > 0)
                {
                    result.List = db.ProjectInfo.Where(whereLambda)
                        .OrderByDescending(o => o.CreateTime)
                        .Skip(pageSize * (pageIndex - 1))
                        .Take(pageSize).ToList();
                }
            }
            return result;
        }
    }
}
